package top.fzqblog.kotlinapp.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.activity_house_detail.*
import top.fzqblog.kotlinapp.R
import top.fzqblog.kotlinapp.func.applySchedulers
import top.fzqblog.kotlinapp.func.log
import top.fzqblog.kotlinapp.http.ApiRetrofit
import top.fzqblog.kotlinapp.http.HouseService
import top.fzqblog.kotlinapp.model.HouseDetail
import top.fzqblog.kotlinapp.util.GlideImgLoader
import top.fzqblog.kotlinapp.util.StatusBarUtil



/**
 * Created by 抽离 on 2017/12/24 0024.
 */
class HouseDetailActivity : AppCompatActivity(){

    val  handler = @SuppressLint("HandlerLeak")
    object: Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            imageView.visibility = View.GONE
            banner.visibility = View.VISIBLE
            banner.start()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?){

        super.onCreate(savedInstanceState);
//        StatusBarUtil.setTranslucentForImageView(this, bannerContainer)
        setContentView(R.layout.activity_house_detail)

        initTranslucentBar()

        val intent: Intent = intent
        val houseId: Long = intent.getLongExtra("houseId", 1L)
        val url = intent.getStringExtra("url")
        Glide.with(this)
                .load(url)
                .placeholder(R.mipmap.house_default_pic)
                .into(imageView)
        if (Build.VERSION.SDK_INT >= 21) {
            val imageTransitionName = intent.getStringExtra("transitionName")
            imageView.transitionName = imageTransitionName
        }
        val apiRetrofit: ApiRetrofit =  ApiRetrofit.getInstance(this)
        val houseService = apiRetrofit.create(HouseService::class.java)

        /**
         * house详情对象
         */
        houseService!!
                .getHouseById(houseId)
                .applySchedulers()
                .subscribe {
                    houseDetail: HouseDetail? ->
                }

        /**
         * 房源图片
         */
        houseService
                .getHouseImgs(houseId)
                .applySchedulers()
                .map {
                    houseImg ->
                    val houseImgList = houseImg.getHouseImgList()
                    houseImgList?.map {
                        houseImgListBean ->
                        houseImgListBean.url
                    }
                }
                .subscribe {
                    urls ->
                    log(urls.toString())
                    banner.setImageLoader(GlideImgLoader())
                    banner.setImages(urls)
                }
        handler.sendEmptyMessageDelayed(1, 1000)
    }

    /**
     * 参照firstApp里的MyScrollView
     */
    private fun initTranslucentBar() {
        StatusBarUtil.setTranslucentForImageView(this, 0, bannerContainer)
        val layoutParams = bannerContainer.layoutParams as ViewGroup.MarginLayoutParams
        layoutParams.setMargins(0, -StatusBarUtil.getStatusBarHeight(this), 0, 0)//将底下的banner
//        val layoutParams2 = banner.layoutParams as ViewGroup.MarginLayoutParams
//        layoutParams2.setMargins(0, StatusBarUtil.getStatusBarHeight(this), 0, 0)
    }

    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacksAndMessages(null)
    }

    /**
     * 监听Back键按下事件,方法1:
     * 注意:
     * super.onBackPressed()会自动调用finish()方法,关闭
     * 当前Activity.
     * 若要屏蔽Back键盘,注释该行代码即可
     */
    override fun onBackPressed() {
        imageView.visibility = View.VISIBLE
        banner.visibility = View.GONE
        super.onBackPressed()
    }

}